package com.lu.nio.server;

import cn.hutool.core.util.CharsetUtil;
import com.lu.GlobalConfig;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * @program lu-netty
 * @description:
 *  - 多个请求 对应一个选择器 对应一个线程
 *  - 选择器定时轮训处理事件
 * @author: zhanglu
 * @create: 2022-06-10 16:15:00
 */
@Slf4j
public class NioServer {

    public static void main(String[] args) {
        try {
            Selector selector = Selector.open();
            ServerSocketChannel socketChannel = ServerSocketChannel.open();
            socketChannel.configureBlocking(false);
            socketChannel.socket().bind(new InetSocketAddress(GlobalConfig.LOCAL_PORT), 1024);
            socketChannel.register(selector, SelectionKey.OP_ACCEPT);
            log.info("NioServer start...");
            new NioServerHandler(selector, CharsetUtil.UTF_8).start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
